package com.chaoyi.util.upload;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONObject;

public class UploadRes {
	
	public static Success success(){
		return new Success();
	}
	
	public static Error error(){
		return new Error();
	}
	
	public static JSONObject error(String msg){
		return new Error(msg).toJSON();
	}
	
	public static class Success{
		private String fileUrl;
		private String delelteUrl;
		private FileType fileType;
		private JSONObject extraParam = new JSONObject();
		
		public Success setFileUrl(String fileUrl) {
			this.fileUrl = fileUrl;
			return this;
		}
		
		public Success setFileType(FileType fileType) {
			this.fileType = fileType;
			return this;
		}

		public Success setDelelteUrl(String delelteUrl) {
			this.delelteUrl = delelteUrl;
			return this;
		}
		
		public Success putExtraParam(String key,String value) {
			extraParam.put(key, value);
			return this;
		}
		
		public JSONObject toJSON(){
			JSONObject result = new JSONObject();
			List<String> fileList = new ArrayList<String>();
			fileList.add(parseFileHtml(fileUrl,fileType));
			result.put("initialPreview", fileList);
			List<JSONObject> configList = new ArrayList<JSONObject>();
			JSONObject config = new JSONObject();
			config.put("url", delelteUrl);
			config.put("extra", extraParam);
			configList.add(config);
			result.put("initialPreviewConfig", configList);
			return result;
		}
		
	}
	/**
	 * 根据文件类型返回展现形式
	 * @param fileUrl
	 * @param fileType
	 * @return
	 */
	private static String parseFileHtml(String fileUrl,FileType fileType){
		String fileHtml = null;
		if(fileType!=null){
				switch(fileType){
				case IMAGE:
					fileHtml = "<img src='"+fileUrl+"' class='file-preview-image' style='width:auto;height:100px;'>";
					break;
				case VIDEO:
					fileHtml = "<video class='kv-preview-data' width='auto' height='100' controls>\n" +
								"<source src='"+fileUrl+"' >\n DEFAULT_PREVIEW \n" +
								"</video>";
					break;
				case OBJECT:
					fileHtml = "<div class='file-preview-text' width='auto' height='100'>" +
						    "<h2><i class='glyphicon glyphicon-file'></i></h2>" + fileUrl + "</div>";
				default:
					fileHtml = "<div class='file-preview-text' width='auto' height='100'>" +
						    "<h2><i class='glyphicon glyphicon-file'></i></h2>apk</div>";
			}
		}else 
			fileHtml = fileUrl;
		return fileHtml;
	}
	
	
	public static class Error{
		private String msg;
		
		public Error(){
			
		}
		
		public Error(String msg){
			this.msg = msg;
		}
		
		public Error setMsg(String msg){
			this.msg = msg;
			return this;
		}
		
		public JSONObject toJSON(){
			JSONObject json = new JSONObject();
			json.put("error", msg);
			return json;
		}
		
	}
	
}
